﻿@model Product
@{
	PageTitle.AddViewTitle("Каталог", Url.Action("Index"));
	
	if (ViewData["Category"] != null)
	{
		List<Category> categoriesForTitle = new List<Category>();
		Category c = (Category)ViewData["Category"];
		while (c != null)
		{
			categoriesForTitle.Insert(0, c);
			c = c.Parent;
		}

		foreach (Category cc in categoriesForTitle)
		{
			PageTitle.AddViewTitle(cc.Title, Url.Action("Index", new { parentId = cc.Id }));
		}
	}
	
	if (Model == null)
	{
		PageTitle.AddViewTitle("Добавление продукта");
	}
	else
	{
		PageTitle.AddViewTitle(Model.Title, Url.Action("Item", new { id = Model.Id }));
		PageTitle.AddViewTitle("Изменение");
	}

	int? selectedCategoryId = ViewData.Eval<int?>("Category.Id");
}

@section head {
	<link rel="stylesheet" type="text/css" href="@Url.Content("~/Modules/Products/Content/Products.less")" />
	<script type="text/javascript">
		$().ready(function () {
			$("#AddImage").click(function () {
				$("ul.newImages").append("<li><img alt='Удалить' title='Удалить' src='/Content/Icons/delete.png' /><input type='file' name='NewImages' /></li>");
				return false;
			});

			$("ul.newImages > li > img").live("click", function () {
				$(this).parents("li").eq(0).remove();
			});

			$("div.picture .delete").click(function () {
				$(this).parents("div.picture").eq(0).remove();
				return false;
			});

			$("div.picture .movePrev").click(function () {
				var div = $(this).parents("div.picture").eq(0);
				var divPrev = div.prev(".picture");

				if (divPrev.length > 0) {
					var input = div.find(".imageSort");
					var inputPrev = divPrev.find(".imageSort");
					var sort = input.val();
					var sortPrev = inputPrev.val();

					input.val(sortPrev);
					inputPrev.val(sort);
					div.after(divPrev);
				}

				return false;
			});

			$("div.picture .moveNext").click(function () {
				var div = $(this).parents("div.picture").eq(0);
				var divNext = div.next(".picture");

				if (divNext.length > 0) {
					var input = div.find(".imageSort");
					var inputNext = divNext.find(".imageSort");
					var sort = input.val();
					var sortNext = inputNext.val();

					input.val(sortNext);
					inputNext.val(sort);
					div.before(divNext);
				}

				return false;
			});
		});
	</script>

	@Html.Partial("Mce", new MceModel(true, (Folder)ViewData["Folder"]))
}


@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) {
	@Html.Hidden("returnUrl")
	@Html.ValidationSummary()
	<p><span class="red">*</span> - обязательные поля</p>
	<fieldset class="form-horizontal">
		<div class="required">
			@Html.LabelFor(m => m.Title)
			@Html.TextBoxFor(m => m.Title)
		</div>
		<div>
			@Html.LabelFor(m => m.Category)
			<div>
				<select id="@ViewData.TemplateInfo.GetFullHtmlFieldId("Category.Id")" name="@ViewData.TemplateInfo.GetFullHtmlFieldName("Category.Id")">
					@foreach (CategoryLeveled cat in ViewBag.Categories)
					{
						<option value="@cat.Id" @(cat.SubCategories.Count > 0 ? "disabled='disabled'".AsHtml() : null) @(cat.Id == selectedCategoryId ? "selected='selected'".AsHtml() : null))>
							@("---".Multiply(cat.Level)) @cat.Title
						</option>
					}
				</select>
				<div class="hint">Продукт можно добавить только в категорию, у которой нет подкатегорий.</div>
			</div>
		</div>
		<div>
			@Html.LabelFor(m => m.Price)
			<div>
				@Html.TextBoxFor(m => m.Price)
				<div class="hint">Строка, например "32 рубля 60 копеек".</div>
			</div>
		</div>
		<div class="vertical">
			@Html.LabelFor(m => m.TextShort)
			@Html.TextAreaFor(m => m.TextShort, new { @class = "tinymce", style = "height:200px" })
		</div>
		<div class="vertical">
			@Html.LabelFor(m => m.TextFull)
			@Html.TextAreaFor(m => m.TextFull, new { @class = "tinymce", style = "height:350px" })
		</div>
	</fieldset>
	
	<fieldset class="form-horizontal">
		<legend>Картинки</legend>
		<div class="toolbar">
			<a id="AddImage" class="button" href="#">
				<img alt="" src="/Content/Icons/add.png" /> Добавить картинку
			</a>
		</div>
		<ul class="newImages"></ul>
		
		@if (Model != null)
		{
			foreach (ProductImage pi in Model.Pictures)
			{
				<div class="picture">
					@Html.Hidden("ImageSort" + pi.Id, pi.Sort, new { @class = "imageSort" })
					<a href="#" class="delete" title="Удалить"><img src="/Content/Icons/delete.png" alt="Удалить" /></a>
					<a href="#" class="movePrev" title="Перемстить к началу"><img src="/Content/Icons/ArrowLeft.png" alt="Перемстить к началу" /></a>
					<a href="#" class="moveNext" title="Переместить к концу"><img src="/Content/Icons/ArrowRight.png" alt="Переместить к концу" /></a>
					<img src="@Url.ImageEntity(pi, "Picture", 150)" alt="Картинка" />
				</div>
			}
		}
	</fieldset>
	
	<div class="form-buttons">
		<input type="submit" value="@(Model == null ? "Добавить" : "Изменить")" />&nbsp;
		<a href="@ViewData["returnUrl"]">Отменить</a>
	</div>
}